package com.example.controller;


import com.aiseed.dao.ChapterDTO;
import com.aiseed.entity.Chapter;
import com.aiseed.service.ChapterService;
import com.aiseed.utils.Result;
import com.aiseed.utils.ResultEnum;
import com.aiseed.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
*/
@RestController
@CrossOrigin
@RequestMapping("/online/chapter")
public class ChapterController {

    @Autowired
    private ChapterService ChapterService;

    //删除章节的方法
    @DeleteMapping("{chapterId}")
    public Result deleteChapter(@PathVariable String chapterId) {
        boolean result = ChapterService.removeChapterId(chapterId);
        if(result) {
            return ResultUtils.success();
        } else {
            return ResultUtils.error(ResultEnum.ERROR);
        }
    }
    //修改章节的方法
    @PostMapping("updateChapter")
    public Result updateChapter(@RequestBody Chapter Chapter) {
        boolean result = ChapterService.updateById(Chapter);
        if(result) {
            return ResultUtils.success();
        } else {
            return ResultUtils.error(ResultEnum.ERROR);
        }
    }
    //根据章节id查询
    @GetMapping("getChapterInfo/{chapterId}")
    public Result getChapterInfo(@PathVariable String chapterId) {
        Chapter Chapter = ChapterService.getById(chapterId);
        return ResultUtils.success("Chapter",Chapter);
    }

    // 添加章节的方法
    @PostMapping("addChapter")
    public Result addChapter(@RequestBody Chapter Chapter) {
        boolean save = ChapterService.save(Chapter);
        if(save) {
            return ResultUtils.success();
        } else {
            return ResultUtils.error(ResultEnum.ERROR);
        }
    }

    //根据id查询查询章节和小节
    @GetMapping("getChapterVideoList/{cid}")
    public Result getChapterVideoListCourseId(@PathVariable String cid) {
        List<ChapterDTO> list = ChapterService.getChapterVideoListByCourseId(cid);
        return  ResultUtils.success("items",list);
    }

}

